package binaryTree;

import java.util.Scanner;

/**统计二叉树叶子结点的个数
 * 
 * */
public class CountLeafNode<AnyType> {
	
	/**求叶子节点的个数*/
	public int countLeaf(BTNode<AnyType> rootNode){
		int leafLeft=0,leafRight=0;
		if(rootNode==null){
			return 0;
		}
		if(rootNode.getLeftChild()==null && rootNode.getRightChild()==null){
			return 0;
		}
		leafLeft=countLeaf(rootNode.getLeftChild());
		leafRight=countLeaf(rootNode.getRightChild());
		return leafLeft+leafRight+1;
	}
	
	//测试函数
	public static void main(String[] args){
		BinaryTree bt1=new BinaryTree();
		Scanner in=new Scanner(System.in);
		System.out.println("请输入字符串，以先序的顺序创建创建二叉树：");
		//供测试数字串 ：abd   ce  f  
		String str=in.nextLine();
		/**将字符串转换成字符数组*/
		char[] tree=str.toCharArray();
		/**创建包装类的数组*/
		Character[] preOrder = new Character[tree.length];
		/**遍历数组，景数组中非空格的字符包装后，存入包装类数组，对空格字符赋值为null*/
		for(int i=0;i<tree.length;i++){
			if(tree[i]==' '){
				preOrder[i]=null;
			}else{
//				preOrder[i]=new Character(tree[i]);
				preOrder[i]=tree[i];
			}
		}
		/**创建二叉树*/
		bt1.setNode(((BinaryTree) bt1).createBTByPreOrder(preOrder));
		
		int num=new CountLeafNode().countLeaf(bt1.getNode());
		System.out.println("该二叉树的叶子节点的个数为："+num);
					
	}	
}
